package com.xxxx.seckill.controller;

import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IGoodsService;
import com.xxxx.seckill.service.UserService;
import com.xxxx.seckill.vo.GoodsVo;
import com.xxxx.seckill.vo.RespBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author:王亚栋
 * @Date:2023/2/14 21:58
 * @Version:1.0
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private UserService userService;

    @Autowired
    private IGoodsService goodsService;

    /**
     * 跳转商品列表页面
     * @param model
     * @param user
     * @return
     */
    @GetMapping("/toList")
    public String toList(User user,
                         Model model){

        model.addAttribute("user",user);

        model.addAttribute("goodsList",goodsService.findGoodsVo());

        return "goodsList";
    }

    @GetMapping("/toDetail/{goodsId}")
    public String goodsDetail(@PathVariable Long goodsId,
                              Model model,
                              User user){

        System.out.println("goodsDetail页面:" + user);
        model.addAttribute("user",user);

        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);

        Date startDate = goodsVo.getStartDate();

        Date endDate = goodsVo.getEndDate();

        Date nowDate = new Date();

        //秒杀状态
        int secKillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;

        //秒杀还未开始
        if (nowDate.before(startDate)){

            remainSeconds = (int)((startDate.getTime() - nowDate.getTime()) / 1000);

        }else if (nowDate.after(endDate)){

            //秒杀已结束
            secKillStatus = 2;
            remainSeconds = -1;
        }else {

            secKillStatus = 1;
            remainSeconds = 0;
        }

        model.addAttribute("goods",goodsVo);
        model.addAttribute("secKillStatus",secKillStatus);
        model.addAttribute("remainSeconds",remainSeconds);

        return "goodsDetail";
    }

}
